package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.Goods;
import com.example.buildtable.pojo.Orders;
import com.example.common.util.R;
import com.example.buildtable.pojo.OrderGoods;
import com.example.buildtable.service.OrderGoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "订单商品")
@RequestMapping(value = "/orderGoods/")
public class OrderGoodsController {
    @Autowired
    private OrderGoodsService orderGoodsService;
    
    @Operation(summary = "查询所有订单商品数据")
    @GetMapping(value = "/getAllOrderGoods")
    public R getAllOrderGoods() {
        LambdaJoinQueryWrapper<OrderGoods> wrapper = new LambdaJoinQueryWrapper<>(OrderGoods.class);
        wrapper.innerJoin(Orders.class, Orders::getOrderId, OrderGoods::getOrderId)
                .innerJoin(Goods.class, Goods::getGoodsId, OrderGoods::getGoodsId);
        List<OrderGoods> orderGoodsList = orderGoodsService.list(wrapper);
        return R.ok("查询所有订单商品数据成功").data("orderGoodsList", orderGoodsList);
    }
    
    @Operation(summary = "增加订单商品")
    @PostMapping(value = "/addOrderGoods")
    public R addOrderGoods(OrderGoods orderGoods) {
        boolean flag = orderGoodsService.save(orderGoods);
        if (flag) {
            return R.ok("订单商品增加成功");
        } else {
            return R.ok("订单商品增加失败");
        }
    }

    @Operation(summary = "删除订单商品")
    @GetMapping(value = "/deleteOrderGoods/{orderGoodsId}")
    public R deleteOrderGoods(@PathVariable("orderGoodsId") Integer orderGoodsId) {
        boolean flag = orderGoodsService.removeById(orderGoodsId);
        if (flag) {
            return R.ok("订单商品删除成功");
        } else {
            return R.ok("订单商品删除失败");
        }
    }

    @Operation(summary = "按照订单商品查看")
    @GetMapping(value = "/getOrderGoodsById/{orderGoodsId}")
    public R getOrderGoodsById(@PathVariable("orderGoodsId") Integer orderGoodsId) {
        LambdaJoinQueryWrapper<OrderGoods> wrapper = new LambdaJoinQueryWrapper<>(OrderGoods.class);
        wrapper.innerJoin(Orders.class, Orders::getOrderId, OrderGoods::getOrderId)
                .innerJoin(Goods.class, Goods::getGoodsId, OrderGoods::getGoodsId);
        wrapper.eq(OrderGoods::getOrderGoodsId, orderGoodsId);
        OrderGoods orderGoods = orderGoodsService.getOne(wrapper);
        if (orderGoods != null) {
            return R.ok("按照订单商品查看成功").data("orderGoods", orderGoods);
        } else {
            return R.error("按照订单商品查看失败");
        }
    }

    @Operation(summary = "修改订单商品")
    @PostMapping(value = "/updateOrderGoods")
    public R updateOrderGoods(OrderGoods orderGoods) {
        boolean flag = orderGoodsService.updateById(orderGoods);
        if (flag) {
            return R.ok("订单商品修改成功");
        } else {
            return R.ok("订单商品修改失败");
        }
    }
}